/*
 * BuscarCheque.java
 *
 * Created on 20/08/2010, 15:06:14
 * Copyright Alex C.
 */
package Soligee.view;


import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeAlterarImplements;
import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeBuscarImplements;
import Soligee.decorador.DecoradorInicializaCombos;
import Soligee.model.vo.lancamentos.ChequeVO;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlDataTable;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import com.icesoft.faces.component.ext.HtmlSelectOneRadio;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectedData;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectionItems;
import com.icesoft.faces.component.jsfcl.data.DefaultTableDataModel;
import com.icesoft.faces.component.jsfcl.data.SelectInputDateBean;
import com.icesoft.faces.component.panelpopup.PanelPopup;
import com.icesoft.faces.component.selectinputdate.SelectInputDate;
import com.sun.rave.faces.converter.SqlDateConverter;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;


/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 */
public class BuscarCheque extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
    private int __placeholder;
    private static List <ChequeVO> listaDeCheques;
    private SessionFacadeBuscarImplements facadeBusca = new SessionFacadeBuscarImplements();
    private SessionFacadeAlterarImplements facadeAlterar = new SessionFacadeAlterarImplements();
        private ChequeVO cheque;
    private static int controle=1;

    /*
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private DefaultTableDataModel dataTable1Model = new DefaultTableDataModel();

    public DefaultTableDataModel getDataTable1Model() {
        return dataTable1Model;
    }

    public void setDataTable1Model(DefaultTableDataModel dtdm) {
        this.dataTable1Model = dtdm;
    }
    private DefaultSelectedData selectOneMenu1Bean = new DefaultSelectedData();

    public DefaultSelectedData getSelectOneMenu1Bean() {
        return selectOneMenu1Bean;
    }

    public void setSelectOneMenu1Bean(DefaultSelectedData dsd) {
        this.selectOneMenu1Bean = dsd;
    }

    private HtmlInputText campoNumeroCheque = new HtmlInputText();

    public HtmlInputText getCampoNumeroCheque() {
        return campoNumeroCheque;
    }

    public void setCampoNumeroCheque(HtmlInputText hit) {
        this.campoNumeroCheque = hit;
    }

    private HtmlInputText campoAgencia = new HtmlInputText();

    public HtmlInputText getCampoAgencia() {
        return campoAgencia;
    }

    public void setCampoAgencia(HtmlInputText hit) {
        this.campoAgencia = hit;
    }
    private HtmlInputText campoNumeroConta = new HtmlInputText();

    public HtmlInputText getCampoNumeroConta() {
        return campoNumeroConta;
    }

    public void setCampoNumeroConta(HtmlInputText hit) {
        this.campoNumeroConta = hit;
    }
    private HtmlInputText campoEmitente = new HtmlInputText();

    public HtmlInputText getCampoEmitente() {
        return campoEmitente;
    }

    public void setCampoEmitente(HtmlInputText hit) {
        this.campoEmitente = hit;
    }
    private HtmlInputText campoObservacao = new HtmlInputText();

    public HtmlInputText getCampoObservacao() {
        return campoObservacao;
    }

    public void setCampoObservacao(HtmlInputText hit) {
        this.campoObservacao = hit;
    }
    private SelectInputDate dataLancamento = new SelectInputDate();

    public SelectInputDate getDataLancamento() {
        return dataLancamento;
    }

    public void setDataLancamento(SelectInputDate sid) {
        this.dataLancamento = sid;
    }
    private SelectInputDate dataDeposito = new SelectInputDate();

    public SelectInputDate getDataDeposito() {
        return dataDeposito;
    }

    public void setDataDeposito(SelectInputDate sid) {
        this.dataDeposito = sid;
    }
    private HtmlInputText campoCodCheque = new HtmlInputText();

    public HtmlInputText getCampoCodCheque() {
        return campoCodCheque;
    }

    public void setCampoCodCheque(HtmlInputText hit) {
        this.campoCodCheque = hit;
    }
    private HtmlSelectOneMenu comboBanco = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboBanco() {
        return comboBanco;
    }

    public void setComboBanco(HtmlSelectOneMenu hsom) {
        this.comboBanco = hsom;
    }
    private PanelPopup panelPopup = new PanelPopup();

    public PanelPopup getPanelPopup() {
        return panelPopup;
    }

    public void setPanelPopup(PanelPopup pp) {
        this.panelPopup = pp;
    }
    private HtmlOutputText textoPopUp = new HtmlOutputText();

    public HtmlOutputText getTextoPopUp() {
        return textoPopUp;
    }

    public void setTextoPopUp(HtmlOutputText hot) {
        this.textoPopUp = hot;
    }
    private HtmlCommandButton btnPopUp2 = new HtmlCommandButton();

    public HtmlCommandButton getBtnPopUp2() {
        return btnPopUp2;
    }

    public void setBtnPopUp2(HtmlCommandButton hcb) {
        this.btnPopUp2 = hcb;
    }
    private HtmlCommandButton btnPopUp1 = new HtmlCommandButton();

    public HtmlCommandButton getBtnPopUp1() {
        return btnPopUp1;
    }

    public void setBtnPopUp1(HtmlCommandButton hcb) {
        this.btnPopUp1 = hcb;
    }
    private HtmlCommandButton botaoMudarSituacao = new HtmlCommandButton();

    public HtmlCommandButton getBotaoMudarSituacao() {
        return botaoMudarSituacao;
    }

    public void setBotaoMudarSituacao(HtmlCommandButton hcb) {
        this.botaoMudarSituacao = hcb;
    }
    private HtmlCommandButton btnAlterar = new HtmlCommandButton();

    public HtmlCommandButton getBtnAlterar() {
        return btnAlterar;
    }

    public void setBtnAlterar(HtmlCommandButton hcb) {
        this.btnAlterar = hcb;
    }
    private HtmlCommandButton btnConfirmarAlteracao = new HtmlCommandButton();

    public HtmlCommandButton getBtnConfirmarAlteracao() {
        return btnConfirmarAlteracao;
    }

    public void setBtnConfirmarAlteracao(HtmlCommandButton hcb) {
        this.btnConfirmarAlteracao = hcb;
    }
    private DefaultSelectedData selectOneRadio1Bean = new DefaultSelectedData();

    public DefaultSelectedData getSelectOneRadio1Bean() {
        return selectOneRadio1Bean;
    }

    public void setSelectOneRadio1Bean(DefaultSelectedData dsd) {
        this.selectOneRadio1Bean = dsd;
    }
    private DefaultSelectionItems selectOneRadio1DefaultItems = new DefaultSelectionItems();

    public DefaultSelectionItems getSelectOneRadio1DefaultItems() {
        return selectOneRadio1DefaultItems;
    }

    public void setSelectOneRadio1DefaultItems(DefaultSelectionItems dsi) {
        this.selectOneRadio1DefaultItems = dsi;
    }
    private HtmlSelectOneRadio radioOpcaoBusca = new HtmlSelectOneRadio();

    public HtmlSelectOneRadio getRadioOpcaoBusca() {
        return radioOpcaoBusca;
    }

    public void setRadioOpcaoBusca(HtmlSelectOneRadio hsor) {
        this.radioOpcaoBusca = hsor;
    }
    private HtmlDataTable tabelaBuscarCheque = new HtmlDataTable();

    public HtmlDataTable getTabelaBuscarCheque() {
        return tabelaBuscarCheque;
    }

    public void setTabelaBuscarCheque(HtmlDataTable hdt) {
        this.tabelaBuscarCheque = hdt;
    }
    private DefaultSelectedData defaultSelectedData1 = new DefaultSelectedData();

    public DefaultSelectedData getDefaultSelectedData1() {
        return defaultSelectedData1;
    }

    public void setDefaultSelectedData1(DefaultSelectedData dsd) {
        this.defaultSelectedData1 = dsd;
    }
    private DefaultSelectionItems selectOneMenu1DefaultItems1 = new DefaultSelectionItems();

    public DefaultSelectionItems getSelectOneMenu1DefaultItems1() {
        return selectOneMenu1DefaultItems1;
    }

    public void setSelectOneMenu1DefaultItems1(DefaultSelectionItems dsi) {
        this.selectOneMenu1DefaultItems1 = dsi;
    }
    private DefaultSelectionItems comboBancoDefaultItems = new DefaultSelectionItems();

    public DefaultSelectionItems getComboBancoDefaultItems() {
        return comboBancoDefaultItems;
    }

    public void setComboBancoDefaultItems(DefaultSelectionItems dsia) {
        this.comboBancoDefaultItems = dsia;
    }
    private HtmlSelectOneMenu comboSitucao = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboSitucao() {
        return comboSitucao;
    }

    public void setComboSitucao(HtmlSelectOneMenu hsom) {
        this.comboSitucao = hsom;
    }
    private HtmlInputText campoValor = new HtmlInputText();

    public HtmlInputText getCampoValor() {
        return campoValor;
    }

    public void setCampoValor(HtmlInputText hit) {
        this.campoValor = hit;
    }
    private SelectInputDateBean selectInputDate1Bean = new SelectInputDateBean();

    public SelectInputDateBean getSelectInputDate1Bean() {
        return selectInputDate1Bean;
    }

    public void setSelectInputDate1Bean(SelectInputDateBean sidb) {
        this.selectInputDate1Bean = sidb;
    }
    private SelectInputDateBean selectInputDateBean2 = new SelectInputDateBean();

    public SelectInputDateBean getSelectInputDateBean2() {
        return selectInputDateBean2;
    }

    public void setSelectInputDateBean2(SelectInputDateBean sidb) {
        this.selectInputDateBean2 = sidb;
    }
    private SelectInputDate dataBusca = new SelectInputDate();

    public SelectInputDate getDataBusca() {
        return dataBusca;
    }

    public void setDataBusca(SelectInputDate sid) {
        this.dataBusca = sid;
    }
    private SqlDateConverter sqlDateConverter1 = new SqlDateConverter();

    public SqlDateConverter getSqlDateConverter1() {
        return sqlDateConverter1;
    }

    public void setSqlDateConverter1(SqlDateConverter sdc) {
        this.sqlDateConverter1 = sdc;
    }

    // </editor-fold>

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public BuscarCheque() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        inicializaCampos();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
            
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("BuscarCheque Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected RequestBean1 getview$RequestBean1() {
        return (RequestBean1) getBean("view$RequestBean1");
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected SessionBean1 getview$SessionBean1() {
        return (SessionBean1) getBean("view$SessionBean1");
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected ApplicationBean1 getview$ApplicationBean1() {
        return (ApplicationBean1) getBean("view$ApplicationBean1");
    }

    public void setarCampos(boolean x){
        campoCodCheque.setDisabled(x);
        campoNumeroCheque.setDisabled(x);
        campoNumeroConta.setDisabled(x);
        comboBanco.setDisabled(x);
        campoAgencia.setDisabled(x);
        campoEmitente.setDisabled(x);
        campoObservacao.setDisabled(x);
        dataDeposito.setDisabled(x);
        dataLancamento.setDisabled(x);
        campoValor.setDisabled(x);
    }

    public void limpaCampos(){
        campoCodCheque.resetValue();
        campoNumeroCheque.resetValue();
        campoNumeroConta.resetValue();
        comboBanco.resetValue();
        campoAgencia.resetValue();
        comboSitucao.resetValue();
        campoEmitente.resetValue();
        campoObservacao.resetValue();
        dataDeposito.resetValue();
        dataLancamento.resetValue();
        campoValor.resetValue();
    }

    public void inicializaCampos(){
        tabelaBuscarCheque.setRendered(false);
        panelPopup.setRendered(false);
        btnConfirmarAlteracao.setRendered(false);
        btnAlterar.setRendered(false);
        selectOneRadio1DefaultItems.clear();
        selectOneRadio1DefaultItems.add(new SelectItem(1,"Situação"));
        selectOneRadio1DefaultItems.add(new SelectItem(2,"Data Lançamento"));
        setarCampos(true);
        comboSitucao.setDisabled(true);
        selectOneMenu1DefaultItems1.clear();
            selectOneMenu1DefaultItems1.add(new SelectItem(1,"Devolvido"));
            selectOneMenu1DefaultItems1.add(new SelectItem(2,"Pendente"));
            selectOneMenu1DefaultItems1.add(new SelectItem(3,"Repassado"));
            selectOneMenu1DefaultItems1.add(new SelectItem(4,"Compensado"));
        inicializaComboBanco();
        dataBusca.setRendered(false);
        
        
    }

    public void inicializaComboBanco(){
        comboBancoDefaultItems = new DecoradorInicializaCombos().inicializarCampoBancoConsulta();
    }

    public void radioOpcaoBusca_processValueChange(ValueChangeEvent vce) {
        if(Integer.parseInt(radioOpcaoBusca.getValue().toString())==1){
            tabelaBuscarCheque.setRendered(true);
            dataBusca.setRendered(false);
            listaDeCheques = facadeBusca.buscarTodos("situacao", ChequeVO.class);
            dataTable1Model.setWrappedData(listaDeCheques);
        }
        else{
            tabelaBuscarCheque.setRendered(true);
            dataBusca.setRendered(true);
            dataTable1Model.setWrappedData(null);
        }
    }

    public String linkAction1_action() {
        cheque = (ChequeVO) dataTable1Model.getRowData();
        campoCodCheque.setValue(cheque.getCodCheque());
        campoNumeroCheque.setValue(cheque.getNumCheque());
        campoNumeroConta.setValue(cheque.getNumConta());
        comboBanco.setValue(cheque.getBanco().getCodBanco());
        campoAgencia.setValue(cheque.getAgencia());
        comboSitucao.setValue(cheque.getSituacao());
        campoEmitente.setValue(cheque.getEmitente());
        campoObservacao.setValue(cheque.getObsReferente());
        dataDeposito.setValue(cheque.getDataDeposito());
        dataLancamento.setValue(cheque.getDataLancamento());
        campoValor.setValue(cheque.getValor());

        comboSitucao.setDisabled(true);
        btnConfirmarAlteracao.setRendered(false);
        btnAlterar.setRendered(true);
        return null;
    }

    public String btnAlterar_action() {
        setarCampos(false);
        btnAlterar.setRendered(false);
        btnConfirmarAlteracao.setRendered(true);
        return null;
    }

    public String btnConfirmarAlteracao_action() {
        controle=2;
        controlaPopup(true, "Confirma Alteração?", true, true, "Sim", "Não");
        return null;
    }

    public void controlaPopup(boolean renderedPopup, String textoPoup, boolean btn1Rendered, boolean btn2Rendered,
           String textobtn1, String textobtn2) {

      panelPopup.setRendered(renderedPopup);
      textoPopUp.setValue(textoPoup);
      btnPopUp1.setValue(textobtn1);
      btnPopUp1.setRendered(btn1Rendered);
      btnPopUp2.setRendered(btn2Rendered);
      btnPopUp2.setValue(textobtn2);
   }

    public String botaoMudarSituacao_action() {
        setarCampos(true);
        comboSitucao.setDisabled(false);
        btnAlterar.setRendered(false);
        btnConfirmarAlteracao.setRendered(true);
        return null;
    }

    public String btnPopUp2_action() {
        controlaPopup(false, "", false, false, "", "");
        return null;
    }

    public void atualizaTabelaCheques(){
        radioOpcaoBusca.setValue(1);
        listaDeCheques = facadeBusca.buscarTodos("situacao", ChequeVO.class);
        dataTable1Model.setWrappedData(listaDeCheques);
    }

    public String btnPopUp1_action() {
        if(controle==1){
            return "voltar";
        }
        else
        {
            Date dataLancamento2 = (Date) dataLancamento.getValue();
            Date dataDeposito2 = (Date) dataDeposito.getValue();
            try {
                if(facadeAlterar.alterarCheque(Integer.parseInt(campoCodCheque.getValue().toString()), campoAgencia.getValue().toString(),
                        campoNumeroConta.getValue().toString(), campoNumeroCheque.getValue().toString(),
                        Integer.parseInt(comboSitucao.getValue().toString()), campoEmitente.getValue().toString(),
                        campoObservacao.getValue().toString(), dataLancamento2, dataDeposito2, Double.parseDouble(campoValor.getValue().toString()),
                        Integer.parseInt(comboBanco.getValue().toString()))){
                    
                    controlaPopup(true, "Cheque Alterado com Sucesso!", false, true, "", "Ok");
                    btnConfirmarAlteracao.setRendered(false);
                    atualizaTabelaCheques();
                    limpaCampos();

                }


            } catch (Exception ex) {
                controlaPopup(true,"Erro:" + ex.getMessage() + "caused by: " + ex.getStackTrace(),false,true,"","OK");
                System.err.println("Erro:" + ex.getMessage() + "caused by: " + ex.getStackTrace());
                ex.getStackTrace();
            }
            return null;
        }
        
    }

    public String btn_voltar_action() {
        controle=1;
        controlaPopup(true, "Deseja Sair?", true, true, "Sim", "Não");
        return null;
    }

    public void dataBusca_processValueChange(ValueChangeEvent vce) {
        Date dataABsucar = (Date) dataBusca.getValue();
        dataTable1Model.setWrappedData(facadeBusca.buscarPorDataGeral(dataABsucar,"dataLancamento","codCheque",ChequeVO.class));
    }

    
        
    

}

